package com.awonsome.oss.autoconfigure;

import com.aliyun.oss.OSS;
import com.awonsome.oss.constant.OssConstants;
import com.awonsome.oss.resource.OssStorageProtocolResolver;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import static com.awonsome.oss.constant.OssConstants.OSS_TASK_EXECUTOR_BEAN_NAME;

/**
 * OSS 自动配置
 *
 * @author yangdejun
 * @date 2022/1/5 23:22
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(OSS.class)
@ConditionalOnProperty(name = OssConstants.ENABLED, havingValue = "true", matchIfMissing = true)
public class OssAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public OssStorageProtocolResolver ossStorageProtocolResolver() {
        return new OssStorageProtocolResolver();
    }

    @Bean(name = OSS_TASK_EXECUTOR_BEAN_NAME)
    @ConditionalOnMissingBean
    public ExecutorService ossTaskExecutor() {
        int coreSize = Runtime.getRuntime().availableProcessors();
        return new ThreadPoolExecutor(coreSize, 128, 60, TimeUnit.SECONDS, new SynchronousQueue<>());
    }
}
